package cn.jdy.dzdd.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import cn.jdy.dzdd.entity.DzddSecretaryMail;
import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;

/**
 * @desc 局长信箱
 * @author ningqian
 * @since 20167-6-9
 */
@Service
public class DzddSecretaryMailService extends BaseDao<DzddSecretaryMail>{

	/**
	 * 按条件查询
	 * @param Notice
	 * @return
	 */
	public List<DzddSecretaryMail> query(DzddSecretaryMail mail, String... date) {
		if(mail == null) return super.findAll();
		if(mail.getiDisplayLength() > 0){
		  return super.find(createFilter(mail, date),mail.getiDisplayStart(),mail.getiDisplayLength());
		}
		return super.find(createFilter(mail, date));
	}

	public Long count(DzddSecretaryMail mail, String... date) {
		return super.count(createFilter(mail, date));
	}
	
	/**
	 * 拼装条件
	 * @param Notice
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(DzddSecretaryMail mail, String... date) {
	  DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
	 
	  if(date.length>0 && !StringUtil.isNullOrEmpty(date[0]) && !StringUtil.isNullOrEmpty(date[1])){
	    dc.add(Restrictions.between("createTime", date[0],date[1]));
	  }
	  if(!StringUtil.isNullOrEmpty(mail.getTitle())){
		    dc.add(Restrictions.eq("title", mail.getTitle()));
	  }
	  if(!StringUtil.isNullOrEmpty(mail.getPublicWill())){
		    dc.add(Restrictions.eq("publicWill", mail.getPublicWill()));
	  }
	  if(!StringUtil.isNullOrEmpty(mail.getAuthor())){
	    dc.add(Restrictions.eq("author", mail.getAuthor()));
	  }
	 
	  dc.addOrder(Order.desc("createTime"));
		return dc;
	}
	

	
}
